package com.fingard.dsp.bank.directbank.jhbank02;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.directbank.jhbank02.utils.Decipher;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * @ClassName JHBANK02Balance
 * @Author Zhengzy
 * @Date 2019/12/2 14:51
 * @Version 1.0
 **/
public class JHBANK02Balance extends JHBANK02Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        // 返回给ats的对象
        String cer = getBankConfig().getValueAsString("certificatePath");
        String pfx = getBankConfig().getValueAsString("certificatePfxPath");
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();
        try {
            while (p_req7506.readRecord()) {
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                RetAtsBalDetail detail = new RetAtsBalDetail();
                // 组装报文头
                JHBANK02Header header = new JHBANK02Header();
                header.userid = bankFront.operator;// 操作员号
                header.brchno = getBankConfig().getValueAsString("nodeNo");// 机构代码
                header.servtp = "";// 渠道
                header.prcscd = "YQCZ01";// 交易码
                writeCommenHeader(xmlTextWriter, header);
                // 组装报文体
                xmlTextWriter.writeStartElement("Body");
                xmlTextWriter.writeElementString("tellNo", bankFront.operator);// 操作员号
                String nodeNo = getBankConfig().getValueAsString("nodeNo");// 机构标识
                xmlTextWriter.writeElementString("nodeNo", nodeNo, true);
                xmlTextWriter.writeElementString("tranCode", "YQCZ01");// 交易码
                xmlTextWriter.writeElementString("accountType", "", true);// 账户类型
                xmlTextWriter.writeElementString("accountNo", p_req7506.readedDetail.actNum, true);// 账号
                // tellNo+nodeNo+ tranCode+accountType+ accountNo 字段值字符串连接后，私钥加签
                String allStr = bankFront.operator + nodeNo + "YQCZ01" + "" + p_req7506.readedDetail.actNum;
                // 加签
                String signStr = Decipher.sign(allStr, pfx);
                xmlTextWriter.writeElementString("signature", signStr);
                xmlTextWriter.writeEndDocument();

                // 发送给银行
                String[] bankResult = sendToBank(xmlTextWriter.toXmlString(), cer);

                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                    if (tmpBkReader != null) {// 有返回内容
                        // 获得密文秘钥
                        String bankEncryptData = tmpBkReader.getFirstText("/root/encryptdata");
                        String bankEncryptKey = tmpBkReader.getFirstText("/root/encryptkey");
                        // 解密
                        String decryptData = decrypt(bankEncryptData, bankEncryptKey, pfx);
                        // 验签
                        if (checkBkSign(decryptData, cer)) {// 验签成功
                            StringBuilder tmpDecryptMsg = new StringBuilder();
                            XmlTextReader decryptBkReader = loadBankRespXmlReader(decryptData, tmpDecryptMsg);
                            String hostReturnCode = decryptBkReader.getFirstText("/root/hostReturnCode");
                            if ("0000".equals(hostReturnCode)) {
                                // 保存银行响应信息
                                detail.respCode = "0";
                                detail.actArea = p_req7506.readedDetail.actArea;// 地区码
                                detail.actNum = p_req7506.readedDetail.actNum;// 账号
                                detail.currBal = decryptBkReader.getSimpleText("balance");// 余额
                                detail.availBal = detail.currBal;// 可用余额
                                detail.actCur = convertCurToATS(decryptBkReader.getSimpleText("currentType"));// 币种
                            } else {
                                // 查询指令失败
                                detail.respCode = "-1";
                                detail.respInfo = decryptBkReader.getFirstText("/root/hostErrorMessage");
                            }
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = "验签不通过";
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = bankResult[1];
                }
                retAts.writeRetDetail(detail, p_req7506.isBatch);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
